package net.daum.webtoon.android.dto;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class DTO_Cartoon extends JSonDataObjectBase {

	public String TAG = getClass().getSimpleName();

	public String id;
	public ArrayList<DTO_Artist> artists;
	public String title;
	public ArrayList<DTO_Genre> genres;
	public String titleImage;
	public String englishTitle;
	public String joinArtists;
	public ArrayList<DTO_Category> categories;
	public String introduction;

	public DTO_Cartoon(JSONObject aJsonObject) {
		Iterator<Object> iterator = aJsonObject.keys();

		while (iterator.hasNext()) {
			Object obj = iterator.next();
			if (obj.toString().equalsIgnoreCase("artists")) {
				this.artists = new ArrayList<DTO_Artist>();
				DTO_Artist lArtist = null;
				try {
					JSONArray lartists = aJsonObject.getJSONArray("artists");

					for (int i = 0; i < lartists.length(); i++) {
						lArtist = new DTO_Artist(lartists.getJSONObject(i));
						this.artists.add(lArtist);
					}
				} catch (JSONException e) {
					if (isDebug)
						e.printStackTrace();
				}

			} else if (obj.toString().equalsIgnoreCase("genres")) {
				this.genres = new ArrayList<DTO_Genre>();
				DTO_Genre lGenre = null;
				try {
					JSONArray lgenres = aJsonObject.getJSONArray("genres");

					for (int i = 0; i < lgenres.length(); i++) {
						lGenre = new DTO_Genre(lgenres.getJSONObject(i));
						this.genres.add(lGenre);
					}
				} catch (JSONException e) {
					if (isDebug)
						e.printStackTrace();
				}

			} else if (obj.toString().equalsIgnoreCase("categories")) {
				this.categories = new ArrayList<DTO_Category>();
				DTO_Category lCategory = null;
				try {
					JSONArray lcategories = aJsonObject.getJSONArray("categories");

					for (int i = 0; i < lcategories.length(); i++) {
						lCategory = new DTO_Category(lcategories.getJSONObject(i));
						this.categories.add(lCategory);
					}
				} catch (JSONException e) {
					if (isDebug)
						e.printStackTrace();
				}
			} else {
				setMemberFiled(this, aJsonObject, obj.toString());
			}
		}

		printMembers();
		insertObjToDB();
	}

}
